package com.common.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class WebDriverUtil {
	
    protected static WebDriver driver;

    public static  WebDriver initWebDriver() throws Exception{

        //得到浏览器配置信息
        int remoteOrNot = BrowserSetupConfig.REMOTEORNOT;
        String browser = BrowserSetupConfig.BROWSER;
        String ieDir = BrowserSetupConfig.IEDIR;
        String firefoxDir = BrowserSetupConfig.FIREFOXDIR;
        String chromeDir = BrowserSetupConfig.CHROMEDIR;
        String IP = BrowserSetupConfig.REMOTEIP;
        String bro = browser.toUpperCase();
//        String chromeOption= BrowserSetupConfig.CHROMEOPTION;
        DesiredCapabilities dc = null;

        //判断浏览器配置不为空
        if(null != browser && browser.trim().length() != 0){
            //在本地跑的情况
            if(remoteOrNot==0){
                if(bro.equals("FIREFOX")){
                    System.setProperty ("webdriver.firefox.bin", firefoxDir);
                    driver = new FirefoxDriver();
                }else if(bro.equals("CHROME")){
                    System.setProperty("webdriver.chrome.driver", chromeDir);
                    //加载chrome本地个人配置
                     driver = new ChromeDriver();
                }else if(bro.equals("IE")){
                    System.setProperty("webdriver.ie.driver", ieDir);
                    dc = DesiredCapabilities.internetExplorer();
                    dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
                    dc.setCapability("ignoreProtectedModeSettings", true);
                    driver = new InternetExplorerDriver(dc);
                }else{
                	handlefailure("浏览器类型错误");
                }
            //在远程跑的情况
            }else if(remoteOrNot==1){
                if(bro.equals("FIREFOX")){
                	FirefoxProfile profile = new FirefoxProfile();
                    dc = DesiredCapabilities.firefox();
                    dc.setCapability("firefox_profile", profile);
                }else if(bro.equals("CHROME")){
                	ChromeOptions options = new ChromeOptions();
                    dc = DesiredCapabilities.chrome();
                    dc.setCapability(ChromeOptions.CAPABILITY, options); 
                }else if(bro.equals("IE")){
                    dc = DesiredCapabilities.internetExplorer();
                    dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
                    dc.setCapability("ignoreProtectedModeSettings", true);
                }else{
                	handlefailure("浏览器类型错误");
                }
                try {
					driver = new RemoteWebDriver(new URL("http://" + IP + ":4444/wd/hub/"),dc);
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }else{
            	handlefailure("类型错误");
            }
        }
        driver.manage().window().maximize();
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        return  driver;
    }
    
    //报出错误，对应案例置为fail
    private static void handlefailure(String msg) throws Exception{
    	throw new Exception(msg);
    }
}
